package com.shengcheng.threadpool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


import java.util.concurrent.*;


@EnableConfigurationProperties(MyThreadProperties.class)
@Configuration
public class MyThreadPool {

    @Autowired
    private MyThreadProperties myThreadProperties;


    @Bean
    public  ThreadPoolExecutor myPoolExecutor(){
        return new ThreadPoolExecutor(
                myThreadProperties.getCorePoolSize(),
                myThreadProperties.getMaximumPoolSize(),
                myThreadProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(myThreadProperties.getQueueLength()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
